package com.example.app01

import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import android.graphics.Color

class MainActivity : AppCompatActivity() {
    private lateinit var languageSpinner: Spinner
    private lateinit var queryButton: Button
    private lateinit var resultTextView: TextView
    private lateinit var controller: ProgrammingAdviceController
    private lateinit var textContainer: LinearLayout
    private var textViewCount = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 初始化MVC组件
        val model = ProgrammingAdviceModel(this)
        controller = ProgrammingAdviceController(model)

        // 初始化视图组件
        initViews()

        // 设置Spinner适配器
        setupSpinner()

        // 设置按钮点击事件
        setupButtonListeners()
    }

    private fun initViews() {
        // 区1组件
        val addTextViewButton = findViewById<Button>(R.id.btn_add_textview)
        textContainer = findViewById(R.id.ll_textview_container)

        // 区2组件
        languageSpinner = findViewById(R.id.spinner_language)
        queryButton = findViewById(R.id.btn_query_advice)
        resultTextView = findViewById(R.id.tv_advice_result)
    }

    private fun setupSpinner() {
        val adapter = ArrayAdapter.createFromResource(
            this,
            R.array.programming_languages,
            android.R.layout.simple_spinner_item
        )
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        languageSpinner.adapter = adapter
    }

    private fun setupButtonListeners() {
        // 添加TextView按钮点击事件
        findViewById<Button>(R.id.btn_add_textview).setOnClickListener {
            addNewTextView()
        }

        // 查询建议按钮点击事件
        queryButton.setOnClickListener {
            val selectedLanguage = languageSpinner.selectedItem.toString()
            val advice = controller.getProgrammingAdvice(selectedLanguage)
            resultTextView.text = advice
        }
    }

    // 动态添加TextView的方法
    private fun addNewTextView() {
        textViewCount++

        val textView = TextView(this).apply {
            text = getString(R.string.dynamic_text, textViewCount)
            textSize = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP,
                16f,
                resources.displayMetrics
            )
            setTextColor(Color.parseColor("#333333"))

            // 设置布局参数和边距
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                setMargins(0, 16, 0, 0) // top margin 16px
            }
        }

        textContainer.addView(textView)
    }
}
